package com.seckill.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.seckill.framework.mybatisplus.WrapperParse;
import com.seckill.goods.mapper.CategoryMapper;
import com.seckill.goods.pojo.Category;
import com.seckill.goods.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品类目 服务实现类
 * </p>
 *
 * @author 作者
 * @since 2022-03-04
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private CategoryMapper categoryMapper;


    /**
     * Category条件+分页查询
     * @param category 查询条件
     * @param page 页码
     * @param size 页大小
     * @return 分页结果
     */
    @Override
    public Page<Category> findPage(Category category, int page, int size){
        //分页
        IPage<Category> iPage = new Page<Category>(page,size);
        //搜索条件构建
        QueryWrapper<Category> wrapper = WrapperParse.simple(category);
        //执行搜索
        return (Page<Category>) categoryMapper.selectPage(iPage,wrapper);
    }

    /**
     * Category分页查询
     * @param page
     * @param size
     * @return
     */
    @Override
    public Page<Category> findPage(int page, int size){
        //静态分页
        IPage<Category> iPage = new Page<Category>(page,size);
        //分页查询
        return (Page<Category>) categoryMapper.selectPage(iPage,null);
    }

    /**
     * Category条件查询
     * @param category
     * @return
     */
    @Override
    public List<Category> findList(Category category){
        //构建查询条件
        QueryWrapper<Category> wrapper = WrapperParse.simple(category);
        //根据构建的条件查询数据
        return categoryMapper.selectList(wrapper);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void delete(Integer id){
        categoryMapper.deleteById(id);
    }

    /**
     * 修改Category
     * @param category
     */
    @Override
    public void update(Category category){
        categoryMapper.updateById(category);
    }

    /**
     * 增加Category
     * @param category
     */
    @Override
    public void add(Category category){
        categoryMapper.insert(category);
    }

    /**
     * 根据ID查询Category
     * @param id
     * @return
     */
    @Override
    public Category findById(Integer id){
        return  categoryMapper.selectById(id);
    }

    /**
     * 查询Category全部数据
     * @return
     */
    @Override
    public List<Category> findAll() {
        return categoryMapper.selectList(null);
    }

    @Override
    public List<Category> findByParentId(Integer id) {
        Map<String,Object> mp = new HashMap<String,Object>();
        mp.put("parent_id",id);
        return categoryMapper.selectByMap(mp);
    }
}
